import * as type from './type'

const mutations = {
    [type.GETCAR](state, action) {
        state.carList = action.payload
    },
    [type.JOINCAR](state, action) {
        // 判断是否为空
        let flag = true
        if (JSON.stringify(state.param).toString() == '[]') {
            state.param.push(action.payload)
        } else {
            // state.param.some(item => {
            //     if (item.code == action.payload.code) {
            //         item.num++
            //             flag = false
            //     }
            // })

            for (var i = 0; i < state.param.length; i++) {
                if (state.param[i].code == action.payload.code) {
                    state.param[i].num++
                        flag = false
                }
            }
            if (flag == true) {
                state.param.push(action.payload)
            }
        }
        // console.log(state.param);
    }
}

export default mutations